//
//  RegisterViewController.m
//  Finder
//
//  Created by cdm on 11-4-12.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"
#import "AppSetting.h"
#import "RemoteService.h"
#import "MBProgressHUD.h"



@interface RegisterViewController ()

- (void)appLanguageDidChange;
- (BOOL)validateForm;

@end


@implementation RegisterViewController

@synthesize phoneLabel;
@synthesize emailLabel;
@synthesize phoneTextField;
@synthesize emailTextField;
@synthesize submitButton;
@synthesize backgroundButton;
@synthesize messageTextView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [self.phoneLabel release];
    [self.emailLabel release];
    [self.phoneTextField release];
    [self.emailTextField release];
    [self.submitButton release];
    [self.backgroundButton release];
    [self.messageTextView release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.phoneLabel.text = [AppLanguage getString:@"Phone"];
    self.emailLabel.text = [AppLanguage getString:@"Email"];
    [self.submitButton setTitle:[AppLanguage getString:@"Submit"] forState:UIControlStateNormal];
    self.messageTextView.text = [AppLanguage getString:@"RegisterMessage"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appLanguageDidChange) name:kAppLanguageChangedNotification object:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.phoneLabel = nil;
    self.emailLabel = nil;
    self.phoneTextField = nil;
    self.emailTextField = nil;
    self.submitButton = nil;
    self.backgroundButton = nil;
    self.messageTextView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Event

- (void)closeButtonItemOnClick:(id)sender
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)submitButtonOnClick:(id)sender
{
    if ([self validateForm]) {
        
        NSString *phone = self.phoneTextField.text;
        NSString *email = self.emailTextField.text;
        
        BOOL result = [[RemoteService sharedService] registerNewsUser:phone email:email];
        
       
        
        if (result) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册成功" message:@"感谢您注册乐享通行证" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];
            [alertView release];
            
            AppSetting *appSetting = [ApplicationContext sharedContext].appSetting;
            appSetting.registered = YES;
            [appSetting saveSetting];
            
            [self.parentViewController dismissModalViewControllerAnimated:YES];
            
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册失败" message:@"注册失败，请稍后重试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];
            [alertView release];
        }
       
    }
   
}

- (void)backgroundButtonOnClick:(id)sender
{
    [self.phoneTextField endEditing:YES];
    [self.emailTextField endEditing:YES];
}

#pragma mark - Validate 

- (BOOL)validateForm
{
    NSString *msg = @"";
    NSString *TEL=self.phoneTextField.text;
    NSString *Email=self.emailTextField.text;
    
    if ([self.phoneTextField.text isEqualToString:@""]) {
        msg = [msg stringByAppendingString:@"请输入电话\r"];
    }
    
    if ([self.emailTextField.text isEqualToString:@""]) {
        msg = [msg stringByAppendingString:@"请输入邮箱\r"];
    }
    
    if([TEL length]!=11){
        msg = [msg stringByAppendingString:@"手机号码错误\r"];

    }
    if([Email rangeOfString:@"@"].location!=NSNotFound){

    }else{
        msg = [msg stringByAppendingString:@"邮箱地址错误\r"];
    }
    
    if ([msg length] > 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提交失败" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        [alertView release];
        
        return NO;
    } else {
        return YES;
    }
}

#pragma mark - App Language

- (void)appLanguageDidChange
{
    self.phoneLabel.text = [AppLanguage getString:@"Phone"];
    self.emailLabel.text = [AppLanguage getString:@"Email"];
    [self.submitButton setTitle:[AppLanguage getString:@"Submit"] forState:UIControlStateNormal];
    self.messageTextView.text = [AppLanguage getString:@"RegisterMessage"];
}

@end
